package io.lckgroup.modules.lck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lckgroup.common.utils.PageUtils;
import io.lckgroup.common.utils.Query;
import io.lckgroup.common.utils.RedisUtils;
import io.lckgroup.modules.lck.dao.OrderMainDao;
import io.lckgroup.modules.lck.entity.OrderMainEntity;
import io.lckgroup.modules.lck.entity.appvo.OrderMainEntityVo;
import io.lckgroup.modules.lck.service.OrderMainService;
import io.lckgroup.modules.job.dao.ScheduleJobLogDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("orderMainService")
public class OrderMainServiceImpl extends ServiceImpl<OrderMainDao, OrderMainEntity> implements OrderMainService {

    @Autowired
    private ScheduleJobLogDao scheduleJobLogDao;

    @Autowired
    private OrderMainDao orderMainDao;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderMainEntity> page = this.page(
                new Query<OrderMainEntity>().getPage(params),
                new QueryWrapper<OrderMainEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByEntity(Map<String, Object> params, OrderMainEntity orderMainEntity) {

        IPage<OrderMainEntity> page = this.page(
                new Query<OrderMainEntity>().getPage(params),
                new QueryWrapper<OrderMainEntity>(orderMainEntity).orderByDesc("gmt_create")
        );

        return new PageUtils(page);
    }


    @Override
    public Map<String, Object> getStaticCountData(Map<String, Object> params) {

        Map<String, Object> today = orderMainDao.getTodayCountData(params);

        Map<String, Object> week = orderMainDao.getWeekCountData(params);

        Map<String, Object> month = orderMainDao.getMonthCountData(params);

        Map<String, Object> map = new HashMap<String, Object>();
        map.putAll(today);
        map.putAll(week);
        map.putAll(month);
        return map;
    }


    @Override
    public Map<String, Object> getDynamicCountData(Map<String, Object> p) {
        Map<String, Object> result = orderMainDao.getDynamicCountData(p);
        return result;
    }

    @Override
    public OrderMainEntityVo getMsgDefineById(String id) {
        OrderMainEntityVo r = orderMainDao.getMsgDefineById(id);
        return r;
    }

    @Override
    @Transactional
    public int cancelOrderTask() {
        List<OrderMainEntity> orders = orderMainDao.selectCancelOrder();
        for (int i = 0; i < orders.size(); i++) {
            redisUtils.delete("weixinGetOrderList::" + orders.get(i).getMerchantId());
        }
        //清除之前的日志记录
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.lt("create_time",new Date());
        queryWrapper.eq("status",0);
        scheduleJobLogDao.delete(queryWrapper);
        if (!orders.isEmpty()) {
            return orderMainDao.cancelOrderTask();
        }
        return 0;
    }

    @Override
    public Long[] selectOrderNumByMonth(Map<String, Object> params) {
        String year = params.get("year").toString();
        int years = Integer.parseInt(year);
        int isPay=3;
        String gmtCreate =year+"-"+"01-01 00:00:00";
        String gmtCreates =(years+1)+"-"+"01-01 00:00:00";
        if(params.get("isPay")!=null){
            isPay=Integer.parseInt(params.get("isPay").toString());
        }
        List<Map<String,String>> list=orderMainDao.selectOrderNumByMonth(isPay,gmtCreate,gmtCreates);
        Long[] OrderNum = new Long[12];

        for (int i = 0; i <12 ; i++) {
            OrderNum[i]=0L;
        }

        for (int i = 0; i <list.size(); i++) {
            int index = Integer.parseInt(String.valueOf(list.get(i).get("month")))-1 ;
            OrderNum[index]=Long.parseLong(String.valueOf(list.get(i).get("OrderNum")));
        }

        return OrderNum;
    }

}